//login js
var _account_service = require('service/account-service.js');
const _util = require('../../util/util');

var loginForm = {
    status: true,
    init: function () {
        $('#verifyimg').attr('src', "http://localhost:8080/account/verifyCode");
        $('#signUp').hide();

        $('#imageVerifyLi').addClass('on');
        $('#phoneVerifyLi').removeClass('on');
        $('#passwordLogin').css("display", 'block');
        $('#phoneLogin').css("display", 'none');

        this.bindEvents();

        return this;
    },
    //点击事件
    bindEvents: function () {
        var _this = this;
        //发送验证码
        $('#sendBtn').on('click', function () {

            var Phone = $('#memPhone').val();
            console.log(Phone);
            if (Phone == '' || Phone.length != 11) {
                alert("请输入正确的手机号！");
            } else {
                data = {
                    memPhone: Phone
                }
                _account_service.sendCode(
                    data,
                    function (resp) {
                        if (data) {
                            alert("发送成功");
                            timer();
                        } else {
                            alert("获取验证码失败");
                        }
                    },
                    function (erroMsg) {
                        alert('连接超时！');
                    }
                )
            }
            var wait = 60;
            //倒计时
            function timer() {
                console.log("waitttt:" + wait);
                if (wait == 0) {
                    $("#sendBtn").val("获取验证码");
                    $("#sendBtn").removeAttr("disabled");
                    $("#sendBtn").css("border-color", "1e9fff").css("background", "#ffffff").css("cursor", "pointer");
                    wait = 60;
                } else {
                    $("#sendBtn").attr("disabled", "true");
                    $("#sendBtn").css("border-color", "fbfbfb").css("background", "#ccc").css("cursor", "not-allowed");
                    $("#sendBtn").val(wait + "秒后重发");
                    wait--;
                    setTimeout(function () { timer() }, 1000);
                }
            }
        });


        //登录
        $('#signon').on('click', function () {
            //采用密码登录

            if (_this.status) {
                _this.loginByPassword();

            }
            //采用短信登录
            else {
                // alert('正在使用短信登录');
                _this.loginByPhone();


            }
        })
        //跳转注册
        $('#signup').on('click', function () {
            $('#login').hide();
            $('#signUp').show();
        })
        //切换验证码
        $('#verifyimg').on('mousedown', function () {
            console.log("hellll img");
            this.src = "http://localhost:8080/account/verifyCode";
        });
        //注册
        $('#signUpSubmit').on('click', function () {
            if ($('#newPassword').val() === $('#repeatedPassword').val()) {
                _account_service.signUp(
                    $('#signUpForm').serialize(),
                    function (resp) {

                        alert("注册成功!欢迎" + resp.username);

                        window.location.href = './catalog-main.html';

                    },
                    function (errorMsg) {
                        alert("注册失败!,请检查您的信息" + errorMsg);
                    }
                )
            }
            else {
                alert("密码不一致");
            }
        })


        // isUsernameExist().then(function (data) {
        //     console.log("waiwaiwa" + data);
        //     $('#usernameTips').attr("class", 'errorMsg').text(data)
        // })

        // async function getTips() {
        //     let getUsernameTips = await isUsernameExist();
        //     if (getUsernameTips) {
        //         let getPasswordTips = await isPasswordValid();
        //     }


        // }
        // $('#username').on('focus', function () {
        //     getTips();
        // });


        $('#newUsername').on('keyup', function () {

            if (this.value === "") {
                console.log("kong");
                $('#newUsernameTips').attr("class", 'errorMsg').text('用户名不可用');
            }
            else {
                _account_service.isUsernameExist(
                    this.value,
                    function (resp) {
                        //存在该用户
                        if (resp === true) {
                            // console.log("用户名可用:");
                            $('#newUsernameTips').attr("class", 'errorMsg').text('用户名不可用');
                            // resolve(true);
                        }
                        else {
                            $('#newUsernameTips').attr("class", 'successMsg').text('用户名可用');
                            // resolve(false);
                        }
                    },
                    function (errorMsg) {
                        console.log(errorMsg);
                    }

                );
            }
        })


        $('#newPassword').on('keyup', function () {
            // console.log(this.value.length)
            if (this.value.length < 16 && this.value.length < 6) {
                console.log("yes");
                $('#newPasswordTips').attr("class", 'successMsg').text('密码可用');
            }
            else {
                console.log("no");
                $('#newPasswordTips').attr("class", 'errorMsg').text('密码长度大于6不超过16');
            }
        })
        $('#repeatedPassword').on('keyup', function () {
            // console.log(this.value);
            // console.log($('#newPassword').val());
            if (this.value === $('#newPassword').val()) {
                console.log("yes");
                $('#repeatedPasswordTips').attr("class", null).text('');

            }
            else {
                console.log("no");
                $('#repeatedPasswordTips').attr("class", 'errorMsg').text('密码不一致');
            }
        })


        $('#imageVerifyLi').on('click', function () {

            _this.status = true;
            $(this).addClass('on');
            $('#phoneVerifyLi').removeClass('on');
            $('#passwordLogin').css("display", 'block');
            $('#phoneLogin').css("display", 'none');
        });
        $('#phoneVerifyLi').on('click', function () {
            _this.status = false;
            $(this).addClass('on');
            $('#imageVerifyLi').removeClass('on');
            $('#passwordLogin').css("display", 'none');
            $('#phoneLogin').css("display", 'block');
        });



    },
    //检验formdatalist 是否有空值
    requireValidation: function (formData) {
        for (let i = 0; i < formData.length; i++) {
            if (!_util.validateField('require', formData[i])) {
                return false;
            }
        }
        return true;
    },
    phoneValidation: function (formData) {

        if (!_util.validateField('phone', formData)) {
            return false;

        }
        return true;
    },

    loginByPhone() {
        var data = {
            phone: $.trim($('#memPhone').val()),
            mcode: $.trim($('#mcode').val()),
        };
        console.log("phone +: " + data.phone);
        if (this.phoneValidation(data.phone) && this.requireValidation([data.mcode])) {
            console.log('phone okk');
            _account_service.loginByPhone(
                data,
                function (resp) {

                    console.log("登录成功bbbb");
                    //跳转页面
                    window.location.href = './catalog-main.html';
                    alert("登陆成功!欢迎" + resp.username);

                },
                function (errorMsg) {
                    alert("登陆失败" + errorMsg);
                    $('#mcode').val('');

                }
            )
        }
        else {
            alert('验证码不能为空');
        }
    },

    loginByPassword() {
        var account = {
            username: $.trim($('#username').val()),
            password: $.trim($('#password').val()),
            code: $('#imageCode').val(),
        };
        var dataList = [account.username, account.password, account.code];
        if (this.requireValidation(dataList)) {
            console.log('验证没问题');
            _account_service.login(
                account,
                function (resp) {
                    console.log("登录成功yyyyyyy");

                    //跳转页面
                    window.location.href = './catalog-main.html';
                    alert("登陆成功!欢迎" + resp.username);

                },
                function (errorMsg) {
                    $('#password').val("");
                    $('#passwordTips').text("");
                    alert("登陆失败" + errorMsg);
                    $('#verifyimg').click();
                    $('#imageCode').val('');
                    // $('#loginTips').attr("class", 'errorMsg').text(errorMsg);
                }
            )
        }
        else {
            alert("表单不能为空");
        }
    }


};
loginForm.init();








